package com.platform.system.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.Constants;
import com.platform.common.constant.HttpStatus;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.core.page.TableDataInfo;
import com.platform.common.enums.ResultEnum;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.bean.BeanUtils;
import com.platform.common.utils.uuid.SnowflakeIdWorker;
import com.platform.system.domain.dto.device.ChatBackgroundMusicDto;
import com.platform.system.domain.dto.device.HumanModelManagerDto;
import com.platform.system.domain.dto.device.SpeakerInfoDto;
import com.platform.system.domain.dto.device.SpeakerTypeDto;
import com.platform.system.domain.entity.device.HumanModelManager;
import com.platform.system.domain.vo.device.HumanModelManagerVo;
import com.platform.system.logic.HumanModelManagerLogic;
import com.platform.system.mapper.device.HumanModelManagerMapper;
import com.platform.system.service.ISysChatBackgroundMusicService;
import com.platform.system.service.ISysHumanModelManagerService;
import com.platform.system.service.ISysSpeakerInfoService;
import com.platform.system.service.ISysSpeakerTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Huangkai
 */
@Service
@Slf4j
public class SysHumanModelManagerServiceImpl extends ServiceImpl<HumanModelManagerMapper, HumanModelManager> implements ISysHumanModelManagerService {

    @Resource
    HumanModelManagerMapper humanModelManagerMapper;

    @Resource
    ISysHumanModelManagerService humanModelManagerService;

    @Autowired
    ISysSpeakerInfoService sysSpeakerInfoService;

    @Resource
    ISysSpeakerTypeService sysSpeakerTypeService;

    @Resource
    ISysChatBackgroundMusicService sysChatBackgroundMusicService;

    @Resource
    HumanModelManagerLogic humanModelManagerLogic;

    @Override
    public TableDataInfo selectHumanModelManagerList(HumanModelManagerVo humanModelManagerVo) {
        LambdaQueryWrapper<HumanModelManager> humanModelManagerLambdaQueryWrapper = new QueryWrapper<HumanModelManager>().lambda();
        humanModelManagerLambdaQueryWrapper.eq(StringUtils.isNotEmpty(humanModelManagerVo.getUserUid()), HumanModelManager::getUserUid, humanModelManagerVo.getUserUid())
                .or()
                .eq(HumanModelManager::getUserUid, 0L);
        humanModelManagerLambdaQueryWrapper.eq(humanModelManagerVo.getStatus() != null, HumanModelManager::getStatus, humanModelManagerVo.getStatus());
        humanModelManagerLambdaQueryWrapper.like(StringUtils.isNotEmpty(humanModelManagerVo.getName()), HumanModelManager::getName, humanModelManagerVo.getName());
        humanModelManagerLambdaQueryWrapper.orderByDesc(HumanModelManager::getCreateTime);
        Page<HumanModelManager> page = new Page<>();
        page.setCurrent(humanModelManagerVo.getPageNum());
        page.setSize(humanModelManagerVo.getPageSize());
        IPage<HumanModelManager> humanModelManagerIPage = humanModelManagerMapper.selectPage(page, humanModelManagerLambdaQueryWrapper);
        TableDataInfo rspData = new TableDataInfo();
        List<HumanModelManager> list =  humanModelManagerIPage.getRecords();
        List<HumanModelManagerDto> humanModelManagerDtoList = new ArrayList<>();
        if(list != null || list.size() > 0){
            humanModelManagerLogic.selectSpeakerInfoListDetail(list, humanModelManagerDtoList);
        }
        rspData.setRows(humanModelManagerDtoList);
        rspData.setTotal(humanModelManagerIPage.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        return rspData;
    }

    @Override
    public List<HumanModelManagerDto> selectHumanModelManagerListByUidList(List<String> uidList) {
        LambdaQueryWrapper<HumanModelManager> humanModelManagerLambdaQueryWrapper = new QueryWrapper<HumanModelManager>().lambda();
        humanModelManagerLambdaQueryWrapper.in(!CollectionUtils.isEmpty(uidList), HumanModelManager::getUid, uidList);
        List<HumanModelManager> humanModelManagerList = humanModelManagerMapper.selectList(humanModelManagerLambdaQueryWrapper);
        if(humanModelManagerList != null){
            String array = JSONArray.toJSONString(humanModelManagerList);
            List<HumanModelManagerDto> humanModelManagerDtoList = JSONArray.parseArray(array, HumanModelManagerDto.class);
            return humanModelManagerDtoList;
        }
        return null;
    }

    @Override
    public List<HumanModelManagerDto> selectHumanModelManagerListByUserId(String userUid) {
        LambdaQueryWrapper<HumanModelManager> humanModelManagerLambdaQueryWrapper = new QueryWrapper<HumanModelManager>().lambda();
        humanModelManagerLambdaQueryWrapper.eq(HumanModelManager::getUserUid, userUid);
        List<HumanModelManager> humanModelManagerList = humanModelManagerMapper.selectList(humanModelManagerLambdaQueryWrapper);
        if(humanModelManagerList != null){
            String array = JSONArray.toJSONString(humanModelManagerList);
            List<HumanModelManagerDto> humanModelManagerDtoList = JSONArray.parseArray(array, HumanModelManagerDto.class);
            return humanModelManagerDtoList;
        }
        return null;
    }

    @Override
    public HumanModelManagerDto selectHumanModelManager(String uid) {
        log.info("详情{}", uid);
        LambdaQueryWrapper<HumanModelManager> humanModelManagerLambdaQueryWrapper = new QueryWrapper<HumanModelManager>().lambda();
        humanModelManagerLambdaQueryWrapper.eq(HumanModelManager::getUid, uid);
        HumanModelManager humanModelManager = humanModelManagerMapper.selectOne(humanModelManagerLambdaQueryWrapper);
        if(humanModelManager != null) {
            HumanModelManagerDto humanModelManagerDto = new HumanModelManagerDto();
            BeanUtils.copyProperties(humanModelManager, humanModelManagerDto);
            if(StringUtils.isNotEmpty(humanModelManager.getOpenQuestion())){
                List<HumanModelManagerDto.QuestionContent> questionContentList = JSONArray.parseArray(humanModelManager.getOpenQuestion(), HumanModelManagerDto.QuestionContent.class);
                humanModelManagerDto.setQuestionContentList(questionContentList);
            }
            if(StringUtils.isNotEmpty(humanModelManager.getParam())){
                List<HumanModelManagerDto.Param> paramList = JSONArray.parseArray(humanModelManager.getParam(), HumanModelManagerDto.Param.class);
                humanModelManagerDto.setFieldList(paramList);
            }
            if(StringUtils.isNotEmpty(humanModelManager.getSpeakerUid())){
                SpeakerInfoDto speakerInfoDto = sysSpeakerInfoService.selectSpeakerInfo(humanModelManager.getSpeakerUid());
                if(speakerInfoDto != null){
                    humanModelManagerDto.setSpeakerInfoDto(speakerInfoDto);
                }
                if(StringUtils.isNotEmpty(speakerInfoDto.getStyleUid())){
                    SpeakerTypeDto speakerTypeDto = sysSpeakerTypeService.selectSpeakerType(speakerInfoDto.getStyleUid());
                    if(speakerInfoDto != null){
                        humanModelManagerDto.setSpeakerTypeDto(speakerTypeDto);
                    }
                }
            }
            if(StringUtils.isNotEmpty(humanModelManager.getChatBackgroundMusicUid())){
                ChatBackgroundMusicDto chatBackgroundMusicDto = sysChatBackgroundMusicService.selectChatBackgroundMusic(humanModelManager.getChatBackgroundMusicUid());
                if(chatBackgroundMusicDto != null){
                    humanModelManagerDto.setChatBackgroundMusicDto(chatBackgroundMusicDto);
                }
            }

            return humanModelManagerDto;
        }
        return null;
    }

    @Override
    public AjaxResult updateHumanModelManagerStatus(HumanModelManagerVo humanModelManagerVo) {
        HumanModelManagerDto humanModelManagerDto = humanModelManagerService.selectHumanModelManager(humanModelManagerVo.getUid());
        if(humanModelManagerDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        HumanModelManager humanModelManager = new HumanModelManager();
        humanModelManager.setStatus(Constants.NUM_ZERO);
        if(humanModelManagerDto.getStatus() == Constants.NUM_ZERO){
            humanModelManager.setStatus(Constants.NUM_ONE);
        }
        LambdaQueryWrapper<HumanModelManager> statusUpdateWrapper = new QueryWrapper<HumanModelManager>().lambda();
        statusUpdateWrapper.eq(HumanModelManager::getUid, humanModelManagerVo.getUid());
        humanModelManager.setUpdateTime(new Date());
        humanModelManager.update(statusUpdateWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult insertHumanModelManager(HumanModelManagerVo humanModelManagerVo) {
        HumanModelManager humanModelManager = new HumanModelManager();
        humanModelManager.setUid(SnowflakeIdWorker.getSnowflakeIdWorker());
        humanModelManager.setName(humanModelManagerVo.getName());
        List<HumanModelManagerVo.QuestionContent> questionContentList = humanModelManagerVo.getQuestionContentList();
        if(!CollectionUtils.isEmpty(questionContentList)){
            String openQuestion = JSONArray.toJSONString(questionContentList);
            humanModelManager.setOpenQuestion(openQuestion);
        }

        List<HumanModelManagerVo.Param> paramList = humanModelManagerVo.getFieldList();
        if(paramList != null){
            String param = JSONArray.toJSONString(paramList);
            humanModelManager.setParam(param);
        }

        humanModelManager.setSpeakerUid(humanModelManagerVo.getSpeakerUid());
        humanModelManager.setChatBackgroundMusicUid(humanModelManagerVo.getChatBackgroundMusicUid());
        humanModelManager.setPrologue(humanModelManagerVo.getPrologue());
        humanModelManager.setWakeUpName(humanModelManagerVo.getWakeUpName());
        humanModelManager.setPromptWord(humanModelManagerVo.getPromptWord());
        humanModelManager.setUserUid(humanModelManagerVo.getUserUid());
        humanModelManager.setCreateTime(new Date());
        humanModelManager.setUpdateTime(new Date());
        humanModelManager.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteHumanModelManager(HumanModelManagerVo humanModelManagerVo) {
        HumanModelManagerDto humanModelManagerDto = humanModelManagerService.selectHumanModelManager(humanModelManagerVo.getUid());
        if(humanModelManagerDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<HumanModelManager> humanModelManagerLambdaQueryWrapper = new QueryWrapper<HumanModelManager>().lambda();
        humanModelManagerLambdaQueryWrapper.eq(HumanModelManager::getUid, humanModelManagerVo.getUid());
        humanModelManagerMapper.delete(humanModelManagerLambdaQueryWrapper);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult updateHumanModelManager(HumanModelManagerVo humanModelManagerVo) {
        log.info("修改的信息{}", humanModelManagerVo);
        HumanModelManagerDto humanModelManagerDto = humanModelManagerService.selectHumanModelManager(humanModelManagerVo.getUid());
        if(humanModelManagerDto == null){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<HumanModelManager> humanModelManagerLambdaQueryWrapper = new QueryWrapper<HumanModelManager>().lambda();
        humanModelManagerLambdaQueryWrapper.eq(HumanModelManager::getUid, humanModelManagerVo.getUid());
        HumanModelManager humanModelManager = new HumanModelManager();
        BeanUtils.copyProperties(humanModelManagerVo, humanModelManager);
        humanModelManager.setUpdateTime(new Date());
        //问题
        List<HumanModelManagerVo.QuestionContent> questionContentList = humanModelManagerVo.getQuestionContentList();
        log.info("文图参数{}", questionContentList);
        if(questionContentList != null){
            String openQuestion = JSONArray.toJSONString(questionContentList);
            humanModelManager.setOpenQuestion(openQuestion);
        }
        List<HumanModelManagerVo.Param> paramList = humanModelManagerVo.getFieldList();
        if(paramList != null){
            String param = JSONArray.toJSONString(paramList);
            humanModelManager.setParam(param);
        }
        humanModelManager.update(humanModelManagerLambdaQueryWrapper);
        return AjaxResult.success();
    }
}
